package com.example.service.deposit.service.impl;

import com.example.service.api.dto.CustomerDTO;
import com.example.service.api.service.AccountDubboService;
import com.example.service.api.service.CustomerDubboService;
import com.example.service.deposit.entity.OpenAccountParam;
import com.example.service.deposit.service.IOpenAccountService;
import com.sun.xml.internal.ws.policy.privateutil.RuntimePolicyUtilsException;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/*
@author SupUncleBlack
@date 2021/8/31 - 17:17
*/
@Service

public class OpenAccountServiceImpl implements IOpenAccountService {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(version = "1.0.0", retries = 0, timeout = 10000)
    private CustomerDubboService customerDubboService;
    @DubboReference(version = "1.0.0", retries = 0, timeout = 10000)
    private AccountDubboService accountDubboService;

    @Override
    @GlobalTransactional
    public String openAccount(OpenAccountParam data) {
        CustomerDTO customerDTO = customerDubboService.getCustomerByIdNumber(data.getIdNumber());

        if(customerDTO == null){
            logger.info("客户信息为空");
            //新建客户信息
            customerDTO = new CustomerDTO();
            customerDTO.setIdNumber(data.getIdNumber());
            customerDTO.setName(data.getName());

            customerDTO = customerDubboService.insertCustomer(customerDTO);
        }

        String customerId = customerDTO.getCustomerId();
        String accountId = accountDubboService.insertAccount(customerId,
                data.getPassword(), data.getPhoneNumber());

        if(data.getPhoneNumber().equals("10")){
            throw new RuntimeException("报错");
        }

        logger.info("创建成功", accountId);

        return customerId;
    }
}
